﻿namespace Leetcode.N0744;

public class Solution
{
    public char NextGreatestLetter(char[] letters, char target)
    {
        var from = 0;
        var to = letters.Length - 1;
        var pos = from;
        while (from < to)
        {
            var step = (to - from + 1) / 2;
            pos = from + step;
            if (letters[pos] <= target)
            {
                from = pos + 1;
            }
            else
            {
                to = pos - 1;
            }
        }

        if (from >= letters.Length)
        {
            return letters[0];
        }

        if (letters[from] > target)
        {
            return letters[from];
        }
        else
        {
            if (from == letters.Length - 1)
            {
                return letters[0];
            }
            else
            {
                return letters[from + 1];
            }
        }
    }


}
